package com.haiwei.poi.word.test;

import com.haiwei.poi.common.HaiweiArrayUtil;
import com.haiwei.poi.word.bean.HaiweiBeanUtil;
import com.haiwei.poi.word.common.HaiweiDocumentGenerator;
import com.haiwei.poi.word.poi.HaiweiParagraphUtil;
import com.haiwei.poi.word.poi.HaiweiXWPFDocument;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;

/**
 * 1.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class test2 {
    
    public static void main(String[] args) {
        try {
            File localFile = new File(FilePathUtils.getPath("22.docx"));
            HaiweiXWPFDocument document =new HaiweiXWPFDocument(new FileInputStream(localFile));
        
            Map<String, Object> map = new HashMap<>();
            map.put("aa","aa111");
            Map<String, Object> map1 = new HashMap<>();
            map.put("客户",map1);
            map.put("customer",map1);
            map1.put("名称","客户名称");
            map1.put("customerName","customer1111");
            HaiweiBeanUtil.setBean(map);
        
            final XWPFComment[] comments = document.getComments();
            if(HaiweiArrayUtil.isNotBlank(comments)){
                for(XWPFComment comment : comments){
                    comment.getText();
                }
            }
    
            final List<POIXMLDocumentPart> relations = document.getRelations();
            final List<POIXMLDocumentPart.RelationPart> relationParts = document.getRelationParts();
            if(HaiweiArrayUtil.isNotBlank(relationParts)){
                for(POIXMLDocumentPart.RelationPart rp : relationParts){
                    String relation = rp.getRelationship().getRelationshipType();
                    if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
                        CommentsDocument cmntdoc = CommentsDocument.Factory.parse(document.getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
                        for (CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
                            ctcomment.setId(new BigInteger("10"));
                            document.addComment(new XWPFComment(ctcomment, document));
                        }
                    }
                }
            }
    
            // 处理
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            HaiweiParagraphUtil.pause(paragraphs);
        
            final File file = new File(FilePathUtils.getPath("2222.docx"));
            HaiweiDocumentGenerator.toFile(document,file);
            System.out.println(file.getAbsolutePath());
        
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
